package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.entity.OrderBase;
import com.bto.solar.work.entity.OrderState;
import com.bto.solar.work.lease.LeaseApi;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王小波
 * @description:
 * @date 2023/8/16 13:58
 */

@Slf4j
@Component
public class LeaseSignedHandler implements StageHandler {
    @Autowired
    private IOrderBaseService orderBaseService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    @Lazy
    private DesignStageHandler designStageHandler;
    @Autowired
    @Lazy
    private BeiAnZhengXingxiHandler beiAnZhengXingxiHandler;
    @Autowired
    private LeaseApi leaseApi;

    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId , BizStageEnum.LEASE_SIGN.getStageId() , BizTaskEnum.TASK_HTQY.getTaskId(), BizTaskStateEnum.WAITING_SIGN.getStateId());
    }

    /**
    * 越秀签约
     * 签约的前置状态：待签约、带重新签约
    */
    @Transactional
    @Override
    public void signedState(String orderId, String stageId, String taskId) {
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.LEASE_SIGN.getStageId(),  BizTaskEnum.TASK_HTQY.getTaskId(),
                BizTaskStateEnum.SIGNED.getStateId(),
                BizTaskStateEnum.WAITING_SIGN.getStateId(),
                BizTaskStateEnum.WAITING_SIGN_RE.getStateId());
        //签约成功开启-设计阶段
        designStageHandler.createNewInitState(orderId, BizStageEnum.DESIGN.getStageId(), BizTaskEnum.TASK_CSPSXX.getTaskId());
        //签约成功开启-备案证阶段
        beiAnZhengXingxiHandler.createNewInitState(orderId, BizStageEnum.BAZING.getStageId(), BizTaskEnum.TASK_BAZXX.getTaskId());
    }

//    @Transactional(rollbackFor = Exception.class)
    public void inceptState(String orderId, String stageId, String taskId) {
        OrderBase orderBase = orderBaseService.getById(orderId);
        if(NullUtils.isNull(orderBase)){
            throw new BizException("订单{} 未找到，无法推送购售电合同" , orderId);
        }
        //购售电合同推送越秀
        OrderState orderState = btoOrderStateService.getOrderState(orderId, BizStageEnum.GS_CONTRACT.getStageId(),
                BizTaskEnum.TASK_GSDHTXX.getTaskId(),
                BizTaskStateEnum.INCEPT_WAITING_PUSH_GSDHT.getStateId());
        if(NullUtils.isNotNull(orderState)){
            boolean pushLease = leaseApi.powerSellOlFileContractPushLease(orderBase);
            if(!pushLease){
                throw new BizException("订单{} 购售电合同推送资方失败，请重试" , orderId);
            }
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.GS_CONTRACT.getStageId(),
                    BizTaskEnum.TASK_GSDHTXX.getTaskId(),
                    BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId() , BizTaskStateEnum.INCEPT_WAITING_PUSH_GSDHT.getStateId());
        }


        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.LEASE_SIGN.getStageId(),  BizTaskEnum.TASK_HTQY.getTaskId(),
                BizTaskStateEnum.INCEPT.getStateId(),
                BizTaskStateEnum.SIGNED.getStateId());




        //签约成功开启-设计阶段
//        designStageHandler.createNewInitState(orderId, BizStageEnum.DESIGN.getStageId(), BizTaskEnum.TASK_CSPSXX.getTaskId());
        //签约成功开启-备案证阶段
//        beiAnZhengXingxiHandler.createNewInitState(orderId, BizStageEnum.BAZING.getStageId(), BizTaskEnum.TASK_BAZXX.getTaskId());
    }


    /**
     * 越秀重签合同
     */
    public void leaseReSigned(String orderId) {
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.LEASE_SIGN.getStageId() ,  BizTaskEnum.TASK_HTQY.getTaskId() ,
                BizTaskStateEnum.WAITING_SIGN_RE.getStateId(),
                BizTaskStateEnum.SIGNED.getStateId()  , true);
    }

}
